package com.augurit.wf.common.convertor;

import java.util.ArrayList;
import java.util.List;

import org.springside.modules.web.struts2.Struts2Utils;

import com.augurit.common.system.web.org.form.AcRoleForm;
import com.augurit.common.system.web.org.form.OmOrgForm;
import com.augurit.common.system.web.org.form.OmPositionForm;
import com.augurit.common.system.web.org.form.OmUserForm;
import com.augurit.wf.jbpmex.assignee.AssigneeTreeNode;
import com.augurit.wf.jbpmex.assignee.AssigneeUtils;

public class SelectUserConvertor {
	
	public static List<AssigneeTreeNode> convertToOrgChildren(List<OmOrgForm> childOrgs, List<OmUserForm> users){
		if((childOrgs != null && childOrgs.size() > 0)
				|| (users != null && users.size() > 0)){
			
			List<AssigneeTreeNode> result = new ArrayList();
			
			List<AssigneeTreeNode> childOrgNodes = convertOmOrgFormList(childOrgs);
			if(childOrgNodes != null && childOrgNodes.size() > 0)
				result.addAll(childOrgNodes);
			
			List<AssigneeTreeNode> userNodes = convertOmUserFormList(users);
			if(userNodes != null && userNodes.size() > 0)
				result.addAll(userNodes);
			
			return result;
		}
		return null;
	}
	
	public static List<AssigneeTreeNode> convertToPosChildren(List<OmPositionForm> childPositions, List<OmUserForm> users){
		if((childPositions != null && childPositions.size() > 0)
				|| (users != null && users.size() > 0)){
			
			List<AssigneeTreeNode> result = new ArrayList();
			
			List<AssigneeTreeNode> childPosNodes = convertOmPositionFormList(childPositions);
			if(childPosNodes != null && childPosNodes.size() > 0)
				result.addAll(childPosNodes);
			
			List<AssigneeTreeNode> userNodes = convertOmUserFormList(users);
			if(userNodes != null && userNodes.size() > 0)
				result.addAll(userNodes);
			
			return result;
		}
		return null;
	}
	
	public static AssigneeTreeNode convertOmOrgForm(OmOrgForm form){
		if(form != null){
			AssigneeTreeNode result = new AssigneeTreeNode();
			
			result.setType(AssigneeUtils.NODE_TYPE_ORG);
			result.setOrgId(form.getOrgId());
			result.setOrgCode(form.getOrgCode());
			result.setText(form.getOrgName());
			result.setQtip(form.getOrgCode());
			result.setLeaf(false);
			result.setBranch(true);
			result.setIcon(Struts2Utils.getRequest().getContextPath() + AssigneeUtils.ICON_ORG);
			
			return result;
		}
		else
			return null;
	}
	
	public static List<AssigneeTreeNode> convertOmOrgFormList(List<OmOrgForm> list){
		if(list != null && list.size() > 0){
			List<AssigneeTreeNode> result = new ArrayList();
			
			for(OmOrgForm form : list){
				result.add(convertOmOrgForm(form));
			}
			
			return result;
		}else
			return null;
	}
	
	public static AssigneeTreeNode convertAcRoleForm(AcRoleForm form){
		if(form != null){
			AssigneeTreeNode result = new AssigneeTreeNode();
			
			result.setType(AssigneeUtils.NODE_TYPE_ROLE);
			result.setRoleId(form.getRoleId());
			result.setRoleCode(form.getRoleCode());
			result.setText(form.getRoleName());
			result.setQtip(form.getRoleCode());
			result.setLeaf(false);
			result.setBranch(true);
			result.setIcon(Struts2Utils.getRequest().getContextPath() + AssigneeUtils.ICON_ROLE);

			return result;
		}else
			return null;
	}
	
	public static List<AssigneeTreeNode> convertAcRoleFormList(List<AcRoleForm> list){
		if(list != null && list.size() > 0){
			List<AssigneeTreeNode> result = new ArrayList();
			
			for(AcRoleForm form : list){
				result.add(convertAcRoleForm(form));
			}
			
			return result;
		}else
			return null;
	}
	
	public static AssigneeTreeNode convertOmPositionForm(OmPositionForm form){
		if(form != null){
			AssigneeTreeNode result = new AssigneeTreeNode();
			
			result.setType(AssigneeUtils.NODE_TYPE_POS);
			result.setPosId(form.getPosId());
			result.setPosCode(form.getPosCode());
			result.setText(form.getPosName());
			result.setQtip(form.getPosCode());
			result.setLeaf(false);
			result.setBranch(true);
			result.setIcon(Struts2Utils.getRequest().getContextPath() + AssigneeUtils.ICON_POS);
			
			return result;
		}
		else
			return null;
	}
	
	public static List<AssigneeTreeNode> convertOmPositionFormList(List<OmPositionForm> list){
		if(list != null && list.size() > 0){
			List<AssigneeTreeNode> result = new ArrayList();
			
			for(OmPositionForm form : list){
				result.add(convertOmPositionForm(form));
			}
			
			return result;
		}else
			return null;
	}
	
	public static AssigneeTreeNode convertOmUserForm(OmUserForm form){

		if(form != null){
			AssigneeTreeNode result = new AssigneeTreeNode();
			
			result.setType(AssigneeUtils.NODE_TYPE_USER);
			result.setUserId(form.getUserId());
			result.setUserKey(form.getLoginName());
			result.setText(form.getUserName());
			result.setQtip(form.getUserCode());
			
			if(form.getUserId().longValue() == AssigneeUtils.CONSTANT_NODE_ID){
				result.setLeaf(false);
				result.setBranch(true);
			}else{
				result.setLeaf(true);
				result.setBranch(false);
			}
			
			if(form.getSex() !=null && form.getSex().equals("F"))
				result.setIcon(Struts2Utils.getRequest().getContextPath() + AssigneeUtils.ICON_USER_FEMALE);
			else
				result.setIcon(Struts2Utils.getRequest().getContextPath() + AssigneeUtils.ICON_USER_MALE);

			return result;
		}else
			return null;
	}
	
	public static List<AssigneeTreeNode> convertOmUserFormList(List<OmUserForm> list){
		if(list != null && list.size() > 0){
			List<AssigneeTreeNode> result = new ArrayList();
			
			for(OmUserForm form : list){
				result.add(convertOmUserForm(form));
			}
			
			return result;
		}else
			return null;
	}
	
}
